<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class ProjectBind extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取车主审批列表
     *
     * @return void
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getList()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $type = $this->request->get('type'); // 0:待审批 1:已审批  2:已拒绝
        $uid = $this->auth->id;
        if (!$uid) {
            $this->error('请先登录');
        }
        if (!$page || !$limit) {
            $this->error('参数错误');
        }
        $project_ids = (new \app\admin\model\ProjectManager())->where('uid', $uid)->column('project_id');
        $model = new \app\admin\model\ProjectBind();
        $list = $model->whereIn('project_id', $project_ids)
                      ->where('status', $type)
                      ->order('id', 'desc')
                      ->page($page, $limit)
                      ->select();
        if (!$list) {
            $this->error('暂无数据');
        }
        foreach ($list as $key => $value) {
            $list[$key]['project_name'] = (new \app\admin\model\Project())->where('id', $value['project_id'])->value('name');
            $list[$key]['car_counts'] = (new \app\admin\model\Car())->where('uid', $value['uid'])->count();
            $list[$key]['owner'] = (new \app\admin\model\Owner())->where('uid', $value['uid'])->find();
        }
        $count = $model->whereIn('project_id', $project_ids)->where('status', $type)->count();
        $data = [
            'list' => $list,
            'count' => $count,
        ];
        $this->success('获取成功', $data);
    }

    /**
     * 审核
     *
     * @return void
     */
    public function audit()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');

        if (!$id || !$type) {
            $this->error('参数错误');
        }

        $model = new \app\admin\model\ProjectBind();
        $data = $model->where('id', $id)->find();
        if (!$data) {
            $this->error('数据不存在');
        }
        if ($data['is_audit'] == 2) {
            $this->error('该申请已通过');
        }
        $updateData = [];

        switch ($type) {
            case 1:
                $updateData = ['status' => 1, 'is_audit' => 1];
                break;
            case 2:
                $updateData = ['status' => 2, 'is_audit' => 2];
                break;
            default:
                $this->error('不支持的审核类型');
        }

        $res = $model->where('id', $id)->update($updateData);

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }



}